/**
 * FileName: ClientV0
 * Author:   powersi
 * Date:     2021/2/26 10:12
 * Description:
 */
package com.zm.demo.netty.action.server;

import com.zm.demo.netty.action.server.codec.OrderFrameDecoder;
import com.zm.demo.netty.action.server.codec.OrderFrameEncoder;
import com.zm.demo.netty.action.server.codec.OrderProtocolDecoder;
import com.zm.demo.netty.action.server.codec.OrderProtocolEncoder;
import com.zm.demo.netty.action.server.handler.OrderServerProcessHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * Description: 
 * Author:      powersi
 * Date:        2021/2/26 10:12
 */
public class OrderServer {


    public static void main(String[] args) {


        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.channel(NioServerSocketChannel.class);

        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workGroup = new NioEventLoopGroup();

        serverBootstrap.handler(new LoggingHandler(LogLevel.INFO));
        serverBootstrap.group(bossGroup,workGroup);
        serverBootstrap.childHandler(new ChannelInitializer<NioSocketChannel>() {
            @Override
            protected void initChannel(NioSocketChannel ch) throws Exception {
                ch.pipeline().addLast(new OrderFrameEncoder());
                ch.pipeline().addLast(new OrderFrameDecoder());
                ch.pipeline().addLast(new OrderProtocolEncoder());
                ch.pipeline().addLast(new OrderProtocolDecoder());
                ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                ch.pipeline().addLast(new OrderServerProcessHandler());
            }
        });

        try {
            ChannelFuture channelFuture = serverBootstrap.bind(8090).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            workGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
}
